from django.contrib.auth.decorators import login_required
from django.http import HttpResponse
from django.shortcuts import render
from django.contrib.auth import authenticate,login,logout

from user.models import UserInfo


# Create your views here.

def reg_view(request):
    if request.method == 'GET':
        return render(request, 'reg.html')
    elif request.method == 'POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        if not username or not password:
            return HttpResponse('用户名或密码为空')
        user=UserInfo.objects.create_user(username=username,password=password)
        user.save()
        return HttpResponse('注册成功')
def login_view(request):
    if request.method == 'GET':
        return render(request, 'login.html')
    elif request.method == 'POST':
        username=request.POST.get('username')
        password=request.POST.get('password')
        if not username or not password:
            return HttpResponse('用户名或密码为空')
        user=authenticate(authenticate,username=username,password=password)
        if not user:
            return HttpResponse('用户名或密码错误')
        login(request,user)
        return HttpResponse('登录成功')


def logout_view(request):
    logout(request)
    return HttpResponse('退出登录成功')

@login_required
def test_login(request):

    return HttpResponse('测试登录成功')